package insight.web.controllers;

import insight.web.delegates.AjaxJSONDelegate;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.primavera.ServerException;
import com.primavera.integration.client.EnterpriseLoadManager;
import com.primavera.integration.client.bo.BOIterator;
import com.primavera.integration.client.bo.BusinessObjectException;
import com.primavera.integration.client.bo.object.ProjectCode;
import com.primavera.integration.client.bo.object.ProjectCodeType;
import com.primavera.integration.network.NetworkException;

@Controller
public class ProjectCodeController extends GenericController {
	@RequestMapping("/loadProjectCode.htm")
	public ModelAndView loadProjectCode(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String codeName = request.getParameter("code");
		if (codeName != null) {
			AjaxJSONDelegate.setTextResponse(buildProjectCodeJSON(codeName),
					response);
		}
		return null;
	}

	private String buildProjectCodeJSON(String codeName)
			throws BusinessObjectException, ServerException, NetworkException,
			JSONException {
		EnterpriseLoadManager elm = helper.getSession()
				.getEnterpriseLoadManager();
		BOIterator<ProjectCodeType> codeTypeItr = elm.loadProjectCodeTypes(
				new String[] { "Name" }, "Name='" + codeName + "'", null);
		List<IProjectCode> list = new ArrayList<IProjectCode>();
		if (codeTypeItr.hasNext()) {
			ProjectCodeType codeType = codeTypeItr.next();
			BOIterator<ProjectCode> codeItr = codeType
					.loadRootProjectCodes(new String[] { "Description",
							"ObjectId" });
			while (codeItr.hasNext()) {
				ProjectCode code = codeItr.next();
				IProjectCode iCode = new IProjectCode(code);
				list.add(iCode);
				addChildern(code, iCode);
			}
		}
		Gson gson = new Gson();
		return gson.toJson(list);
	}

	private void addChildern(ProjectCode parentCode, IProjectCode parentICode)
			throws BusinessObjectException, ServerException, NetworkException {
		BOIterator<ProjectCode> codeItr = parentCode.loadProjectCodeChildren(
				new String[] { "Description", "ObjectId" }, null, null);
		while (codeItr.hasNext()) {
			ProjectCode code = codeItr.next();
			IProjectCode iCode = new IProjectCode(code);
			parentICode.addChildern(iCode);
			addChildern(code, iCode);
		}
	}

	public static class IProjectCode {
		private int id;
		private String text;
		private String closed;
		private List<IProjectCode> children;

		public IProjectCode(ProjectCode code) throws BusinessObjectException {
			setId(code.getObjectId().toInteger());
			setText(code.getDescription());
			setClosed("true");
		}

		public void addChildern(IProjectCode code) {
			if (children == null) {
				children = new ArrayList<IProjectCode>();
			}
			children.add(code);
		}

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getText() {
			return text;
		}

		public void setText(String text) {
			this.text = text;
		}

		public String getClosed() {
			return closed;
		}

		public void setClosed(String closed) {
			this.closed = closed;
		}
	}
}
